//
//  EditNotesVC.m
//  优学荟管理系统
//
//  Created by 星道 on 17/6/29.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "EditNotesVC.h"
#import "EditNotesCell.h"
#import "ImgZoom.h"

@interface EditNotesVC ()<EditNotesCellDelegate>
{
    // 临时保存退后的按钮
    UIBarButtonItem *_LeftBarButtonItem;

    // 是否界面消失时显示提示
    BOOL _IsShowTag;
}
@end

@implementation EditNotesVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 该界面隐藏Navigation的左按钮
    _LeftBarButtonItem = self.navigationItem.leftBarButtonItem;
    // 清空左按钮
    self.navigationItem.leftBarButtonItem = nil;
    // 隐藏后退按钮
    self.navigationItem.hidesBackButton = YES;

}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    // 显示界面的Navigation的左按钮
    self.navigationItem.leftBarButtonItem = _LeftBarButtonItem;


    // 判断是否显示
    if (_IsShowTag)
    {
        [HUD showSuccess:@"重复上传将覆盖上一次批注记录" toView:[UIApplication sharedApplication].keyWindow.rootViewController.view];
    }
}



- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = @"编辑批注";

    // 配置TableView
    [self SetupTableView];

}



#pragma mark  - 配置
/**
 配置TableView
 */
- (void)SetupTableView
{
    // 占位
    self.tableView.tableFooterView = [UIView new];
    self.tableView.tableHeaderView = [UIView new];
    // 预注册
    [self.tableView registerNib:[UINib nibWithNibName:[EditNotesCell description] bundle:nil] forCellReuseIdentifier:@"Cell"];
    // 预估高度
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 600;

}




#pragma mark - 网络请求
/**
 网络请求：上传批注
 */
- (void)RefreshData_Content:(NSString *)Content
{

    NSDictionary *Params = @{
                             @"reply"   :Content,
                             @"action"  :@"savePostil",
                             @"id"      :self.Model.ID
                             };
    
    [NetManager Post:LLBaseURLs(@"coursePackage.do") Auth:YES Params:Params Success:^(id Obj) {

        //判断是否加载成功
        NSDictionary *Dict = [NSJSONSerialization JSONObjectWithData:Obj options:NSJSONReadingMutableLeaves error:nil];
        if ([Dict[@"code"] isEqualToNumber:@1000])
        {
            [HUD showSuccess:Dict[@"msg"]];

            // 处理成功的操作
            [self performSelector:@selector(MgrSccess) withObject:nil afterDelay:1.5];
        }
        else
        {
            [HUD showError:Dict[@"msg"]];
        }
    } Fail:^(NSError *Error) {

        //显示错误信息
        [HUD showError:NetErr];
    }];
}



#pragma mark - 方法
/**
 当内容上传成功处理
 */
- (void)MgrSccess
{
    // 显示提示窗
    _IsShowTag = YES;
    [self.navigationController popViewControllerAnimated:YES];
}







#pragma mark - TableView代理
//区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

//Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 实例Cell
    EditNotesCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    // 调用点语法
    Cell.Model = self.Model;
    Cell.Delegate = self;
    return Cell;
}



#pragma mark - 上传、取消、图片放大方法
/**
 图片放大
 */
- (void)EditNotesCell:(EditNotesCell *)EditNotesCell ClickImageWithButton:(UIButton *)Button
{
    // 开始处理
    [ImgZoom MgrButtonImgView:Button];
}

/**
 取消
 */
- (void)EditNotesCell:(EditNotesCell *)EditNotesCell ClickCancleWithButton:(UIButton *)Button Content:(NSString *)Content
{
    // 得到UserDefault
    NSUserDefaults *Defaults = [NSUserDefaults standardUserDefaults];
    // 检测输入框是否有值，有值的话提示是否保存否则就不保存
    if ( [Content stringByReplacingOccurrencesOfString:@" " withString:@""].length <= 0 )
    {
        // 删除缓存
        [Defaults setObject:@"" forKey:@"EditNotesCellTextView"];
        // 写入
        [Defaults synchronize];
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        // 弹框提示
        UIAlertController *Alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否将您刚刚输入的内容放进草稿中?" preferredStyle:(UIAlertControllerStyleActionSheet)];

        [Alert addAction:[UIAlertAction actionWithTitle:@"保存" style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction * _Nonnull action) {

            // 将内容保存到缓存
            [Defaults setObject:Content forKey:@"EditNotesCellTextView"];
            // 写入
            [Defaults synchronize];
            [self.navigationController popViewControllerAnimated:YES];
        }]];

        [Alert addAction:[UIAlertAction actionWithTitle:@"放弃" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {

            // 清空输入框的缓存
            // 写入内容
            [Defaults setObject:@"" forKey:@"EditNotesCellTextView"];
            // 写入
            [Defaults synchronize];
            [self.navigationController popViewControllerAnimated:YES];
        }]];

        [self presentViewController:Alert animated:YES completion:nil];
    }

}

/**
 上传
 */
- (void)EditNotesCell:(EditNotesCell *)EditNotesCell ClickCommitContentWithButton:(UIButton *)Button Content:(NSString *)Content
{
    // 判断文字是否为空
    if ( [Content stringByReplacingOccurrencesOfString:@" " withString:@""].length <= 0 )
    {
        [HUD showError:@"请编写您的批注"];
        return;
    }
    
    // 开始上传批注
    [self RefreshData_Content:Content];
    
}


#pragma mark - Setter
@end
